In [15]:
%matplotlib notebook

Week 4


In [16]:
import numpy as np
import seaborn as sns
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import csv

In [23]:
data = open('../data/data.csv', 'r').readlines()
fieldnames = ['x', 'y', 'z', 'unmasked', 'synapses']
reader = csv.reader(data)
reader.next()

rows = [[int(col) for col in row] for row in reader]

xs = []
ys = []
zs = []
ss = []

sorted_x = sorted(list(set([r[0] for r in rows])))
sorted_y = sorted(list(set([r[1] for r in rows])))
sorted_z = sorted(list(set([r[2] for r in rows])))

vol = np.zeros((len(sorted_x), len(sorted_y), len(sorted_z)))
for r in rows:
    xs.append(r[0])
    ys.append(r[1])
    zs.append(r[2])
    ss.append(r[4]/(r[3] + .01))
    vol[sorted_x.index(r[0]), sorted_y.index(r[1]), sorted_z.index(r[2])] = r[-1]

1. 3D Heatmap

NOTE: This demo will be slow on low-spec computers (like mine).


In [10]:
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(xs,ys,zs, marker='o', s=ss, c="goldenrod", alpha=0.4)
for ii in xrange(0,360,1):
    ax.view_init(elev=10., azim=ii)
fig.show()


2. Heatmap after thresholding

Here, we assume that there is some level of noise, which can be defined by redefining THRESH below. The same heatmap is generated, but only for values where the synapse count is higher than the threshold, thus attempting to remove noise.


In [24]:
THRESH = np.mean(ss) * 3/2

In [29]:
print len(xs), len(ss)
ss_poppable = []
for s in range(len(ss)):
    if ss[s] < THRESH:
        ss_poppable.append(s)

for s in reversed(ss_poppable):
    xs.pop(s)
    ys.pop(s)
    zs.pop(s)
    ss.pop(s)
    
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(xs,ys,zs, marker='o', r=ss, c="black", alpha=0.8)
for ii in xrange(0,360,1):
    ax.view_init(elev=10., azim=ii)
fig.show()


14649 14649
-------------------------------------------------------------------
AttributeError                    Traceback (most recent call last)
<ipython-input-29-617ae320cdef> in <module>()
     13 fig = plt.figure()
     14 ax = Axes3D(fig)
---> 15 ax.scatter(xs,ys,zs, marker='o', r=ss, c="black", alpha=0.8)
     16 for ii in xrange(0,360,1):
     17     ax.view_init(elev=10., azim=ii)

/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mpl_toolkits/mplot3d/axes3d.pyc in scatter(self, xs, ys, zs, zdir, s, c, depthshade, *args, **kwargs)
   2271         xs, ys, zs, s, c = cbook.delete_masked_points(xs, ys, zs, s, c)
   2272 
-> 2273         patches = Axes.scatter(self, xs, ys, s=s, c=c, *args, **kwargs)
   2274         if not cbook.iterable(zs):
   2275             is_2d = True

/usr/local/lib/python2.7/site-packages/matplotlib/__init__.pyc in inner(ax, *args, **kwargs)
   1810                     warnings.warn(msg % (label_namer, func.__name__),
   1811                                   RuntimeWarning, stacklevel=2)
-> 1812             return func(ax, *args, **kwargs)
   1813         pre_doc = inner.__doc__
   1814         if pre_doc is None:

/usr/local/lib/python2.7/site-packages/matplotlib/axes/_axes.pyc in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs)
   3894                 )
   3895         collection.set_transform(mtransforms.IdentityTransform())
-> 3896         collection.update(kwargs)
   3897 
   3898         if colors is None:

/usr/local/lib/python2.7/site-packages/matplotlib/artist.pyc in update(self, props)
    854                 func = getattr(self, 'set_' + k, None)
    855                 if func is None or not six.callable(func):
--> 856                     raise AttributeError('Unknown property %s' % k)
    857                 func(v)
    858             changed = True

AttributeError: Unknown property r

In [ ]: